/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.prototype.console;

import java.io.IOException;
import java.util.concurrent.Callable;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author namounis
 */
public class SvnJobThread implements Callable<String> {
    /**.
     * @param pageLink
     */
    private String pageLink;
    /**.
     * @param commandLine
     */
    private String commandLine;

    /**.
      * Constructor
      * @param pageLink2
      */
    public SvnJobThread(final String pageLink2) {
        this.pageLink = pageLink2;
        this.commandLine = null;

    }

    /**.
     * recover the command line according to the page's structure
     * @param pageLink
     * @throws IOException
     */
    public void getCli(String pageLink) throws IOException {

        Document sfPage = Jsoup.connect(pageLink).timeout(0).get();
        /* case 1 */
        Elements result = sfPage.select("div .grid-16 code");
        if (!result.text().equals("")) {
            this.commandLine = result.text();
        }
        /* case 2 */
        Elements result1 = sfPage.select("div .yui-u code");
        if (!result1.text().equals("")) {
            this.commandLine = result1.text();
        }
        /* case 3 HTTP*/
        Elements result2 = sfPage.select("div .btn-set a[title$=HTTP]");
        if (!result2.text().equals("")) {
            this.commandLine = result2.attr("data-url");
        }
    }

    /**.
     * recover the page's link to get the command line
     * @return tableau
     * @throws IOException
     */
    public String getCodeURL() throws IOException{
        Document sfPage = Jsoup.connect(this.pageLink).timeout(0).get();
        int i = 0;
        int size = sfPage.select("li a").size();
        while (i < size) {
            Element result = sfPage.select("li a").get(i);
            if (result.text().toString().contains("Code")
                    || result.text().toString().equals("Svn")
                    || result.text().toString().equals("Cvs")
                    || result.text().toString().equals("Git")) {
                getCli(result.absUrl("href"));
            }
            i++;
        }
        return this.commandLine;
    }

    @Override
    public String call() {
        String codeUrl = "";
        try {
            codeUrl = getCodeURL();
        } catch (Exception ex) {
            ex.getMessage();
        }
        return codeUrl;
    }
}
